package com.jmu.edu.IMApplication.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.GestureAndFaceResultItemAdapter;
import com.jmu.edu.IMApplication.bean.GestureAndFaceResult;
import com.jmu.edu.IMApplication.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;


public class CustomTextFragment extends Fragment {
    private RecyclerView recyclerView;
    private MaterialButton backButton;
    private List<GestureAndFaceResult> gestureAndFaceResultList;
    private GestureAndFaceResultItemAdapter adapter;
    private String backTag = null;


    public CustomTextFragment() {
        // Required empty public constructor
    }

    public void setGestureAndFaceResultList(List<GestureAndFaceResult> gestureAndFaceResultList) {
        this.gestureAndFaceResultList = gestureAndFaceResultList;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            setArguments(new Bundle());
        } else {
            loadData();
            ViewUtils.addBackListenerForHide(getView(), backButton);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_custom_text, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        recyclerView = getView().findViewById(R.id.fragment_revise_custom_text_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        backButton = getView().findViewById(R.id.fragment_revise_custom_text_back);
        initListener();
        loadData();
        ViewUtils.addBackListenerForHide(getView(), backButton);
    }

    private void loadData() {
        if (getArguments() != null) {
            backTag = getArguments().getString("backTag", null);
        }
        if (adapter == null) {
            adapter = new GestureAndFaceResultItemAdapter();
        }
        adapter.setGestureAndFaceResultList(gestureAndFaceResultList);
        if (recyclerView.getAdapter() == null) {
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }


    }

    private void initListener() {
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getContext(), "backTag: " + backTag, Toast.LENGTH_SHORT).show();
                if (backTag == null) {
                    ViewUtils.hideFragment(CustomTextFragment.this, R.anim.from_right, R.anim.out_right);
                } else {
                    // 如果有backTag说明是肯定存在这个Fragment的
                    Fragment fragment = MyApplication.fragmentManager.findFragmentByTag(backTag);
                    ViewUtils.hideFragment(CustomTextFragment.this, fragment, backTag, R.anim.from_left, R.anim.out_right);
                }

            }
        });
    }
}